package cn.edu.neusoft.wuguangsheng.market.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.market.bean.CartFoodsBean;
import cn.edu.neusoft.wuguangsheng.market.bean.RegisterBean;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.CartSetFlagModel;
import cn.edu.neusoft.wuguangsheng.market.model.DelectCartModel;
import cn.edu.neusoft.wuguangsheng.market.model.SetCartNumModel;

/**
 * Created by little on 2018/4/23.
 */

public class CartFoodsAdapter extends BaseAdapter<CartFoodsBean> {
    static int allnum;
    private Handler handler;

    public CartFoodsAdapter(Context context, List<CartFoodsBean> items, int layoutResource,Handler handler) {
        super(context, items, layoutResource);

        this.handler=handler;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(layoutResourse, parent, false);
        NewCartCardViewHolder newCartCardViewHolder = new NewCartCardViewHolder(itemView);
        return newCartCardViewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final NewCartCardViewHolder cardViewHolder = (NewCartCardViewHolder) holder;
        final CartFoodsBean cartFoodsBean = items.get(position);
        final String foodname = cartFoodsBean.getFoodname();
        cardViewHolder.ct_foodname.setText(foodname);
        final int nums = cartFoodsBean.getNum();
        final int k = nums;
        allnum = nums;
        final String n = String.valueOf(nums);
        cardViewHolder.ct_num.setText(n);
        final double price = cartFoodsBean.getPrice();
        String p = String.valueOf(price);
        cardViewHolder.ct_price.setText(p);
        final int ck_cart = cartFoodsBean.getFlag();

        if (ck_cart == 1) {
            cardViewHolder.ck_cart.setChecked(true);
        } else {
            cardViewHolder.ck_cart.setChecked(false);
        }

        final int cid = cartFoodsBean.getCid();
        final String pic = cartFoodsBean.getPic();
        final String fid = cartFoodsBean.getFid();
        final String mrid = cartFoodsBean.getMrid();

        cardViewHolder.ct_img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetCartNumModel setCartNumModel=new SetCartNumModel();
                //int num=nums+1;
                final int num=cartFoodsBean.getNum()+1;
                String c=String.valueOf(cid);



                setCartNumModel.setCartNum(c,num,new TListener<RegisterBean>() {
                    @Override
                    public void onResponse(RegisterBean registerBean) {
                        if (registerBean.getSuccess().equals("1")) {
                            Toast.makeText(context, "加载中", Toast.LENGTH_SHORT).show();
                            cartFoodsBean.setNum(num);
                            cardViewHolder.ct_num.setText(num + "");
                            Message message = Message.obtain();
                            message.what = 0;
                            handler.sendMessage(message);
                        } else {
                            Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFail(String msg) {
                    }
                });
            }
        });
     cardViewHolder.ct_img_desc.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             if(cartFoodsBean.getNum()==1){
                final int num=1;
                 String c=String.valueOf(cid);
                 SetCartNumModel setCartNumModel=new SetCartNumModel();

                 setCartNumModel.setCartNum(c, num, new TListener<RegisterBean>() {
                     @Override
                     public void onResponse(RegisterBean registerBean) {
                         if (registerBean.getSuccess().equals("1")) {
                             Toast.makeText(context, "加载中", Toast.LENGTH_SHORT).show();
                             cartFoodsBean.setNum(num);

                             cardViewHolder.ct_num.setText(num + "");
                             Message message = Message.obtain();
                             message.what = 0;
                             handler.sendMessage(message);
                         } else {
                             Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                         }
                     }

                     @Override
                     public void onFail(String msg) {

                     }
                 });

             }
             else if(cartFoodsBean.getNum()>1){
                 final int num=cartFoodsBean.getNum()-1;
                 String c=String.valueOf(cid);
                 SetCartNumModel setCartNumModel=new SetCartNumModel();

                 setCartNumModel.setCartNum(c, num, new TListener<RegisterBean>() {
                     @Override
                     public void onResponse(RegisterBean registerBean) {
                         if (registerBean.getSuccess().equals("1")) {
                             Toast.makeText(context, "加载中", Toast.LENGTH_SHORT).show();
                             cartFoodsBean.setNum(num);

                             cardViewHolder.ct_num.setText(num + "");
                             Message message = Message.obtain();
                             message.what = 0;
                             handler.sendMessage(message);
                         } else {
                             Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                         }
                     }

                     @Override
                     public void onFail(String msg) {

                     }
                 });
             }



         }
     });

        cardViewHolder.ck_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Boolean q = cardViewHolder.ck_cart.isChecked();
                if (q == true) {
                    String c = String.valueOf(cid);
                    String flag = "1";
                    CartSetFlagModel cartSetFlagModel = new CartSetFlagModel();
                    cartSetFlagModel.cartSetFlag(c, flag, new TListener<RegisterBean>() {
                        @Override
                        public void onResponse(RegisterBean registerBean) {


                            if (registerBean.getSuccess().equals("1")) {
                                Toast.makeText(context, "加载中", Toast.LENGTH_SHORT).show();
                                Message message = Message.obtain();
                                message.what = 0;
                                handler.sendMessage(message);
                            } else {
                                Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFail(String msg) {

                        }
                    });
                } else {
                    String c = String.valueOf(cid);
                    String flag = "0";
                    CartSetFlagModel cartSetFlagModel = new CartSetFlagModel();
                    cartSetFlagModel.cartSetFlag(c, flag, new TListener<RegisterBean>() {
                        @Override
                        public void onResponse(RegisterBean registerBean) {
                            if (registerBean.getSuccess().equals("1")) {
                                Toast.makeText(context, "加载中", Toast.LENGTH_SHORT).show();
                                Message message = Message.obtain();
                                message.what = 0;
                                handler.sendMessage(message);
                            } else {
                                Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFail(String msg) {

                        }
                    });
                }


            }
        });

        cardViewHolder.delect_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DelectCartModel delectCartModel=new DelectCartModel();
                String c=String.valueOf(cid);
                delectCartModel.delectCart(c, new TListener<RegisterBean>() {
                    @Override
                    public void onResponse(RegisterBean registerBean) {
                        if (registerBean.getSuccess().equals("1")) {
                            Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                            items.remove(position);
                            //notifyItemMoved(position);
                            notifyItemRemoved(position);
                            notifyItemRangeChanged(0,items.size());

                            Message message = Message.obtain();
                            message.what = 0;
                            handler.sendMessage(message);
                        } else {
                            Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFail(String msg) {

                    }
                });
            }
        });


    }

    TListener<RegisterBean> tListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            String success = registerBean.getSuccess();

            if (success.equals("1")) {
                Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
            }
            else if(success.equals("0")){
                Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(context, "设置失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
        }
    };

TListener<RegisterBean> delectListener=new TListener<RegisterBean>() {
    @Override
    public void onResponse(RegisterBean registerBean) {
        String success=registerBean.getSuccess();
        if (success.equals("1")){
            Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onFail(String msg) {
        Toast.makeText(context,"网络失败",Toast.LENGTH_SHORT).show();
    }
};


TListener<RegisterBean> setaddNumListener=new TListener<RegisterBean>() {
    @Override
    public void onResponse(RegisterBean registerBean) {
        String success=registerBean.getSuccess();
        if(success.equals("1")){
            Toast.makeText(context,"增加成功",Toast.LENGTH_SHORT).show();

        }
        else if(success.equals("0")){
            Toast.makeText(context,"增加失败",Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(context,"返回错误",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(context,"网络错误",Toast.LENGTH_SHORT).show();
    }
};

TListener<RegisterBean> setoddNumListener=new TListener<RegisterBean>() {
    @Override
    public void onResponse(RegisterBean registerBean) {
        String success=registerBean.getSuccess();
        if(success.equals("1")){
            Toast.makeText(context,"减少成功",Toast.LENGTH_SHORT).show();

        }
        else if(success.equals("0")){
            Toast.makeText(context,"减少失败",Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(context,"返回错误",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(context,"网络错误",Toast.LENGTH_SHORT).show();
    }
};
}

